package io.characters;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

import org.junit.Test;

import io.TestResourcesDirectory;

/**
 * 文件输入输出字符流的演示，用于传输包含字符的文件（例如文本）
 * 
 * @author fidel
 */
public class FileDemo {
    @Test
    public void test() {
        File file = new File(TestResourcesDirectory.PATH + File.separator + "CharactersFileDemo.properties");
        try (Writer w = new FileWriter(file);) {
            String lineSeparator = System.getProperty("line.separator");
            w.write("first.property=属性值α" + lineSeparator);
            w.write("2st=属性值β" + lineSeparator);
            w.write("last=属性值γ" + lineSeparator);
            char[] chars = new char[] { 'l', 'a', 'n', 'g', 'u', 'a', 'g', 'e' };
            w.write(chars);
            w.write("=");
            char[] chars2 = new char[] { 'J', 'a', 'v', 'a' };
            for (char aChar : chars2) {
                w.write(aChar);
            }
            w.write(System.lineSeparator());
            w.write("placeholder=你好，{0}，现在是北京时间{1}！" + lineSeparator);
            w.flush();
            Reader r = new FileReader(file);
            int value;
            while ((value = r.read()) > -1) {
                System.out.print((char) value);
            }
            r.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
